﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace nwrole.lib {
    /*public class ServiceChannel : IDisposable, IServiceContract {
        #region IPluginContract wrapper

        public Guid GetIdentifier ()
        {
            return service.GetIdentifier();
        }

        public Result Start() {
            return service.Start();
        }

        public Result Stop() {
            return service.Stop();
        }

        #endregion

        //public Result InstantiatePlugin (Plugin plugin, Solution solution, Solution.Service service, Solution.Instance instance)
        //{
        //    return manager.InstantiatePlugin(plugin, solution, service, instance);
        //}

        //public Result KillInstance (Guid instanceId)
        //{
        //    return plugin.KillInstance(instanceId);
        //}

        #region Common
        private IServiceContract service;

        public ServiceChannel (ServiceData serviceData)
        {
            try
            {
                EndpointAddress address = new EndpointAddress("net.tcp://localhost:" + serviceData.ManagementPort.ToString() + "/" + serviceData.Id.ToString("N"));
                NetTcpBinding binding = new NetTcpBinding();
                binding.ReceiveTimeout = new TimeSpan(0, 0, 120);
                binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
                binding.MaxReceivedMessageSize = 2147483647;
                ChannelFactory<IServiceContract> facory = new ChannelFactory<IServiceContract>(binding, address);
                facory.Credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
                service = facory.CreateChannel();
            }
            catch (Exception exception)
            {
                EventLogger.LogException("+ServiceManagement", exception);
            }
        }

        public void Dispose ()
        {
            try
            {
                ((IClientChannel)service).Close(TimeSpan.FromSeconds(30));
                ((IClientChannel)service).Dispose();
            }
            catch (Exception exception)
            {
                EventLogger.LogException("ServiceManagement.Dispose", exception);
            }
        }

        #endregion
    }*/
}
